package cn.tt.moudles.sys.controller;

import cn.tt.moudles.sys.entity.Demo;
import cn.tt.moudles.sys.entity.User;
import cn.tt.moudles.sys.service.DemoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用来测试用，如果整个Controller都只想返回JSON，直接直接给类注入RestController
 * Created by Nick on 2017/5/6.
 */

@Controller
@RequestMapping("/test")
public class TestController {

	@Autowired
	private DemoService testUserService;

	@RequestMapping("/index")
	public String index(ModelMap map) {
		map.put("host", "127.0.0.1");

		List<User> users = new ArrayList<User>();
		for (int i = 0; i < 10; i++) {
			User user = new User();
			user.setName("用户" + i);
			user.setCreateDate(new Date());
			users.add(user);
		}
		map.put("users", users);

		return "test";
	}

	/**
	 * 分页 PAGE
	 */
	@GetMapping("/selectPage")
	@ResponseBody
	public Page<Demo> selectPage() {
		return testUserService.selectPage(new Page<Demo>(0, 10));
	}

	@GetMapping("/selectList")
	@ResponseBody
	public List<Demo> selectList() {
		return testUserService.selectListBySQL();
	}

	@GetMapping("/addUser")
	@ResponseBody
	public Object addUser(){
		Demo demo = new Demo("张三", 20);
		JSONObject result = new JSONObject();
		result.put("result", testUserService.insert(demo));
		return result;
	}

	@GetMapping("/updateUser")
	@ResponseBody
	public Object updateUser(){
		Demo demo = new Demo(18, "lisi", 23);
		JSONObject result = new JSONObject();
		result.put("result", testUserService.updateById(demo));
		return result;
	}

	@PostMapping("/testBindDate")
	@ResponseBody
	public Object test(@RequestBody Map<String, Object> data) {
		System.out.println(data);
		Map<String, Object> map = new HashedMap();
		map.put("date", new Date());
		return map;
	}


}
